package com.shopyourstyle.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shopyourstyle.R;

public class ProductAdapter extends BaseAdapter {
	private Activity activity;
	private ProductInfo[]  infoArray;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;

	public ProductAdapter(Activity a, ProductInfo[] productinfoArray) {
	        activity = a;
	        infoArray = productinfoArray;
	        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        imageLoader=new ImageLoader(activity.getApplicationContext());
	    }

	@Override
	public int getCount() {
		// TODO Auto-generated method stub

		Log.d("Adapter", "getCount called");
		return infoArray.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		Log.d("Adapter", "getItem called");
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		Log.d("Adapter", "getItemId called");
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.productitem, null);
		
		
		final int cnum = (Integer)infoArray[position].getinfo(ProductInfo.TYPE.COMMENTNUM);
		final int fnum = (Integer)infoArray[position].getinfo(ProductInfo.TYPE.FITNUM);
		final String url = (String)infoArray[position].getinfo(ProductInfo.TYPE.URL);
		final String pname = (String)infoArray[position].getinfo(ProductInfo.TYPE.NAME);
		
		TextView name = (TextView) vi.findViewById(R.id.productname);
		ImageView image = (ImageView) vi.findViewById(R.id.productimg);
		name.setText(pname);
		imageLoader.DisplayImage(url, activity, image);
		image.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(view.getContext(), SingleProductPage.class);
				myIntent.putExtra("name", pname);
				myIntent.putExtra("url", url);
				myIntent.putExtra("commentnum", cnum);
				myIntent.putExtra("fitnum", fnum);
				view.getContext().startActivity(myIntent);
				
			}
			
		});
		TextView comment = (TextView) vi.findViewById(R.id.commentitem);
		TextView fit = (TextView) vi.findViewById(R.id.fititem);
		
		
		if (cnum > 0) {
        	comment.setClickable(true);
        	comment.setFocusable(true);
        	comment.setOnClickListener(new OnClickListener() {

    			@Override
    			public void onClick(View view) {
    				// TODO Auto-generated method stub
    				
    				Log.d("OnClickListener", "onClick called (comment)");
    				Intent myIntent = new Intent(view.getContext(), CommentPage.class);
    				myIntent.putExtra("commentnum", cnum);
    				view.getContext().startActivity(myIntent);

    				
    			}
            	
            });
			if (cnum == 1) {
				comment.setText(cnum + " comment");
			} else {
				comment.setText(cnum + " comments");
			}
		}
		
		if (fnum != 0) {
			fit.setClickable(true);
			fit.setFocusable(true);
			fit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					// TODO Auto-generated method stub

					Log.d("OnClickListener", "onClick called (fit)");
					Intent myIntent = new Intent(view.getContext(),
							FitPage.class);
					myIntent.putExtra("fitnum", fnum);
					view.getContext().startActivity(myIntent);

				}

			});
			if (fnum > 0)
				fit.setText("fit +" + fnum);

			else
				fit.setText("fit " + fnum);
		}

		return vi;
	}

}
